Get data ready:
library(toxEval)
library(dplyr)
library(tidyr)
library(DT)
path_to_tox <- system.file("extdata", package="toxEval")
file_name <- "OWC_data_fromSup.xlsx"
full_path <- file.path(path_to_tox, file_name)
tox_list <- create_toxEval(full_path)
ACClong <- get_ACC(tox_list$chem_info$CAS)
ACClong <- remove_flags(ACClong)
cleaned_ep <- clean_endPoint_info(endPointInfo)
filtered_ep <- filter_groups(cleaned_ep)
chemicalSummary <- get_chemical_summary(tox_list, ACClong, filtered_ep)
#Trim some names:
levels(chemicalSummary$Class)[levels(chemicalSummary$Class) == "Antimicrobial Disinfectants"] <- "Antimicrobial"
levels(chemicalSummary$Class)[levels(chemicalSummary$Class) == "Detergent Metabolites"] <- "Detergent"
levels(chemicalSummary$Class)[levels(chemicalSummary$Class) == "Flavors and Fragrances"] <- "Flavor/Fragrance"
file_name <- "AOP_crosswalk.csv"
full_path <- file.path(path_to_tox, file_name)
AOP_crosswalk <- read.csv(full_path, stringsAsFactors = FALSE)
chemicalSummary <- chemicalSummary %>%
left_join(select(endPointInfo,
endPoint=assay_component_endpoint_name,
subFamily=intended_target_family_sub,
gene_symbol=intended_target_gene_symbol), by="endPoint") %>%
left_join(select(tox_list$chem_info, CAS, `Chemical Name`), by="CAS")tableData <- chemicalSummary %>%
rename(Chemical=`Chemical Name`,
Family=Bio_category) %>%
group_by(site, endPoint, Family, subFamily, gene_symbol, Chemical)
max_Samples <- tableData %>%
summarize(sumEAR = sum(EAR)) %>% #Sum per date
slice(which.max(sumEAR)) %>% # Gets max per date
filter(sumEAR > 0) %>%
data.frame() %>%
spread(Chemical, sumEAR) %>%
arrange(site, Family, subFamily, gene_symbol) %>%
select(site, Family, subFamily, gene_symbol,endPoint, everything()) %>%
mutate(maxSample = rowSums(.[-1:-5], na.rm = TRUE)) %>%
select(site, Family, subFamily, gene_symbol, endPoint, maxSample)
tableData <- tableData %>%
summarize(maxEAR = max(EAR)) %>%
filter(maxEAR > 0) %>%
data.frame() %>%
spread(Chemical, maxEAR) %>%
arrange(site, Family, subFamily, gene_symbol) %>%
select(site, Family, subFamily, gene_symbol,endPoint, everything()) %>%
left_join(select(AOP_crosswalk,
endPoint=Component.Endpoint.Name,
AOP_id = AOP..,
AOP_title = AOP.Title), by="endPoint") %>%
left_join(max_Samples, by=c("site", "Family", "subFamily", "gene_symbol","endPoint")) %>%
select(site, Family, subFamily, gene_symbol, endPoint, AOP_id, AOP_title, maxSample, everything())
list_tables <- list()
chem_site <- tox_list$chem_site
for(i in 1:nrow(chem_site)){
site <- chem_site$SiteID[i]
site_name <- chem_site$`Short Name`[i]
tableData_site <- tableData[tableData$site == site,]
tableData_site <- Filter(function(x)!all(is.na(x)), tableData_site)
list_tables[[2*i-1]] <- htmltools::tags$h3(site_name)
if(nrow(tableData_site) > 0){
tableData2 <- select(tableData_site, -endPoint, -Family, -subFamily, -gene_symbol, -AOP_id, -AOP_title, -site, -maxSample)
tableData_site$nChems <- apply(tableData2, MARGIN = 1, function(x) sum(x>0, na.rm = TRUE))
orderedCols <- tox_list$chem_info$`Chemical Name`[tox_list$chem_info$`Chemical Name` %in% names(tableData_site)]
tableData_site <- tableData_site[,c("Family", "subFamily", "gene_symbol", "endPoint","AOP_id","AOP_title", "maxSample", "nChems", orderedCols)]
dt_table <- datatable(tableData_site, rownames = FALSE,extensions = 'Buttons',
options = list(dom = 'Bfrtip',
buttons = list('colvis', list(
extend = 'collection',
buttons = list(list(extend='csv',
filename = 'siteTable'),
list(extend='excel',
filename = 'siteTable'),
list(extend='pdf',
filename= 'siteTable')),
text = 'Download')
))) %>%
formatSignif(columns=c("maxSample",orderedCols), digits=3)
list_tables[[2*i]] <- dt_table
} else {
list_tables[[2*i]] <- htmltools::tags$h3("EAR never > 0")
}
}htmltools::tagList(
list_tables
)